<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            :clickModalClose="true"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="90px" :rules="formRules">
                <el-form-item label="广告位置" prop="title">
                    <el-input v-model="formData.title" clearable placeholder="请输入广告位置说明" />
                </el-form-item>
                <el-form-item label="容量" prop="num">
                    <el-input v-model="formData.num" clearable placeholder="请输入容量" />
                </el-form-item>

                <el-form-item label="状态" prop="status">
                  <el-select v-model="formData.status" placeholder="请选择状态">
                    <el-option label="暂停" :value="0">暂停</el-option>
                    <el-option label="启用" :value="1">启用</el-option>
                  </el-select>
                </el-form-item>


                <el-form-item label="像素" prop="pixel">
                    <el-input v-model="formData.pixel" clearable placeholder="请输入像素" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { dictDataLists } from '@/api/setting/dict'
import { apiAdvAdd, apiAdvEdit } from '@/api/adv'
import { timeFormat } from '@/utils/util'

const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')

// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑广告位置表' : '新增广告位置表'
})

// 表单数据
const formData = reactive({
    id: '',
    title: '',
    num: '',
    status: '',
    pixel: '',
})

// 字典数据
const dictData = reactive<Record<string, any[]>>({

})

// 获取字典数据
const getDictData = () => {

}

// 表单验证
const formRules = reactive<any>({
    title: [{
        required: true,
        message: '请输入广告位置说明',
        trigger: ['blur']
    }],
    num: [{
        required: true,
        message: '请输入容量',
        trigger: ['blur']
    }],
    status: [{
        required: true,
        message: '请输入状态 0 暂停 1 启用',
        trigger: ['blur']
    }],
    pixel: [{
        required: true,
        message: '请输入像素',
        trigger: ['blur']
    }]
})


// 获取详情
const setFormData = (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }


}


// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData,  }
    mode.value == 'edit'
        ? await apiAdvEdit(data)
        : await apiAdvAdd(data)
    popupRef.value?.close()
    emit('success')
}

//打开弹窗
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

// 关闭回调
const handleClose = () => {
    emit('close')
}

getDictData()

defineExpose({
    open,
    setFormData
})
</script>
